package com.wangzhy.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author wangzhy
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/api/excel-templates/**").addResourceLocations("classpath:/excel-templates/");

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(serviceInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/static/**", "/doc.html", "swagger-ui.html", "/webjars/**");
    }


    /*@Bean
    public ObjectMapper objectMapper() {
        return new ObjectMapper();
    }*/


    /*@Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        return new MappingJackson2HttpMessageConverter(objectMapper());
    }*/


//    @Override
//    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
//        // 拦截 http请求，转换为想要的数据
//        argumentResolvers.add(new QueryParamResolver());
//        argumentResolvers.add(new JsonBodyResolver());
//        argumentResolvers.add(new PageDbResolver());
//        argumentResolvers.add(new JsonWapperResolver());
//        argumentResolvers.add(new ParameterParserResolver());
//    }




    @Bean
    public ServiceInterceptor serviceInterceptor() {
        return new ServiceInterceptor();
    }


    class ServiceInterceptor implements HandlerInterceptor {

        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                               ModelAndView modelAndView) {
//            request.setAttribute("preImageUrl", fileUploadHandle.getPreImageUrl());
        }
    }

}
